package com.lsp.microcommon.common;


import com.lsp.microcommon.constants.CommonConstants;
import lombok.Data;

@Data
public class CommonResult {

    // 返回结果状态，200表示成功，500表示异常
    private int code;
    // 提示信息
    private String msg;
    // 后端返回前端需要绑定的数据
    private Object data;//==list

    /**
     * 调用成功，返回的数据
     *
     * @param data
     * @return
     */
    public static CommonResult success(Object data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonConstants.SUCCESS_CODE);
        commonResult.setData(data);
        commonResult.setMsg("");
        return commonResult;
    }

    public static CommonResult success() {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonConstants.SUCCESS_CODE);
        commonResult.setData(null);
        commonResult.setMsg("");
        return commonResult;
    }

    /**
     * 调用过程中，针对可能出现的异常，返回数据
     *
     * @param msg
     * @return
     */
    public static CommonResult error(String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(CommonConstants.ERROR_CODE);
        commonResult.setData(null);
        commonResult.setMsg(msg);
        return commonResult;
    }

    public static CommonResult error(int code,String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setData(null);
        commonResult.setMsg(msg);
        return commonResult;
    }
}
